package com.atguigu.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.entity.product.ProductSpec;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.mapper.index.ProductDetailsMapper;
import com.atguigu.spzx.product.mapper.index.ProductMapper;
import com.atguigu.spzx.product.mapper.index.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mahone
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Cacheable(value = "product", key = "'all'")
    @Override
    public List<ProductSku> selectAllData() {
        return productSkuMapper.selectAllData();
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer pageNum, Integer pageSize, ProductSkuDto productSkuDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductSku> productSkuList = productMapper.selectProductSkuList(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    @Override
    public ProductItemVo selectItemInfo(Long skuId) {
        // productSku
        ProductSku productSku = productSkuMapper.selectById(skuId);
        // product
        Product product = productMapper.selectBySkuId(productSku.getProductId());
        // 海报
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        // map封装商品规格信息
        // map{key=规格信息,value=规格id}
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(product.getId());
        Map<String,Object> map = new HashMap<>();
        for (ProductSku sku : productSkuList) {
            Long mapSkuId = sku.getId();
            String mapSkuSpec = sku.getSkuSpec();
            map.put(mapSkuSpec,mapSkuId);
        }

       // 封装ProductItemVo
        ProductItemVo productItemVo = new ProductItemVo();
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        //  商品详情图片列表数据格式：array[string]
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        // 商品轮播图数据格式：array[string]
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        // 商品规格信息:  array [object {0}]
       productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
       // 商品规格信息及对应商品skuId信息
       productItemVo.setSkuSpecValueMap(map);
        return productItemVo;
    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        ProductSku productSku = productSkuMapper.selectBySku(skuId);
        return productSku;
    }
}
